#include"mypretreat.h"
#include<fstream>
#include<iostream>
#include<windows.h>
#include<gdiplus.h>
#include<graphics.h>
#include<conio.h>
#pragma comment(lib, "gdiplus.lib")

void load_data(Eigen::MatrixXd& data, Eigen::VectorXd& labels,
	const char* fx, const char* fy)
{
	int data_row = data.rows(), data_col = data.cols();
	std::ifstream inx(fx, std::ios::in), iny(fy, std::ios::in);
	if (!inx.is_open() || !iny.is_open())
		std::cout << "can not open file";
	else {
		for (int j = 0; j < data_row; j++)
			for (int i = 0; i < data_col; i++)
				inx >> data(j, i);
		for (int j = 0; j < data_row; j++)
			iny >> labels(j, 0);
	}
	inx.close();
	iny.close();
}


Eigen::MatrixXd one_hot(int num_labels,Eigen::VectorXd& labels)
{
	int r = labels.rows();
	Eigen::MatrixXd onehot(r, num_labels);
	for (int i = 0; i < r; i++)
	{
		int flag = labels(i, 0);
		for (int j = 0; j < num_labels; j++)
		{
			if (j == flag) onehot(i, j) = 1;
			else onehot(i, j) = 0;
		}
	}
	return onehot;
}


//get pixel from *.jpg using bitmap
//header needed:windows.h and gdiplus.h
void readjpg(const wchar_t* infilename, Eigen::MatrixXd & temp)
{
	using namespace Gdiplus;
	using namespace std;
	GdiplusStartupInput gdiplusstartupinput;
	ULONG_PTR gdiplustoken;
	GdiplusStartup(&gdiplustoken, &gdiplusstartupinput, NULL);


	Bitmap* bmp = new Bitmap(infilename);
	UINT height = bmp->GetHeight();
	UINT width = bmp->GetWidth();
	
	temp = Eigen::MatrixXd::Zero(height, width);

	Color color;
	for (UINT y = 0; y < height; y++)
	{
		for (UINT x = 0; x < width; x++) {
			bmp->GetPixel(x, y, &color);
			//rgb->gray
			temp(y, x) = (0.3 * color.GetRed() + 0.1 * color.GetBlue() + 0.6 * color.GetGreen()) * 1.0 / 255;
		}
	}
	delete bmp;
	GdiplusShutdown(gdiplustoken);
}


//header needed:graphics.h and conio.h
void showjpg(const wchar_t* infilename)
{
	//creat image
	IMAGE img;

	//load image
	loadimage(&img, infilename, 200, 200);

	initgraph(200, 200, NOMINIMIZE);

	//print
	putimage(0, 0, &img);

	//input any key
	_getch();

	//close
	closegraph();
}